package com.naizhuai.util;

import android.text.TextUtils;

import com.naizhuai.Model.db.user.PersonUserBean;
import com.naizhuai.ui.entity.ChargeCheckItemBean;
import com.naizhuai.ui.entity.management.DepartmentBean;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

/**
 * list处理工具
 * Created by code_han on 2018/3/6.
 */

public class ListUtils {

    /**
     * 获取第二个集合不同于第一个集合的集合
     *
     * @param sourceList 原始列表数据
     * @param dataList   更新获取的数据
     * @return
     */
    public static List<PersonUserBean> getListsDifferData(List<PersonUserBean> sourceList, List<PersonUserBean> dataList) {

        for (PersonUserBean sourceUser : sourceList) {
            Iterator<PersonUserBean> sListIterator = dataList.iterator();
            while (sListIterator.hasNext()) {
                if (sourceUser.getUid().equals(sListIterator.next().getUid()))
                    sListIterator.remove();
            }
        }
        return dataList;
    }

    /**
     * 获取第二个集合不同于第一个集合的集合__部门集合
     *
     * @param sourceList 原始列表数据
     * @param dataList   更新获取的数据
     * @return
     */
    public static List<DepartmentBean> getDepartmentListsDifferData(List<DepartmentBean> sourceList, List<DepartmentBean> dataList) {

        for (DepartmentBean sourceUser : sourceList) {
            Iterator<DepartmentBean> sListIterator = dataList.iterator();
            while (sListIterator.hasNext()) {
                if (sourceUser.getDepartmentId().equals(sListIterator.next().getDepartmentId()))
                    sListIterator.remove();
            }
        }
        return dataList;
    }

    /**
     * 获取第二个集合不同于第一个集合的集合__T集合(比较toString)
     *
     * @param sourceList 原始列表数据
     * @param dataList   更新获取的数据
     * @return
     */
    public static <T> List<T> getDataListsDifferData(List<T> sourceList, List<T> dataList) {

        for (T sourceUser : sourceList) {
            Iterator<T> sListIterator = dataList.iterator();
            while (sListIterator.hasNext()) {
                if (sourceUser.toString().equals(sListIterator.next().toString()))
                    sListIterator.remove();
            }
        }
        return dataList;
    }

    /**
     * 判断集合里面是否含有元素
     *
     * @param sourceList
     * @param userBean
     * @return
     */
    public static boolean contain(List<PersonUserBean> sourceList, PersonUserBean userBean) {
        for (PersonUserBean user : sourceList) {
            if (userBean.getUid().equals(user.getUid()))
                return true;
        }
        return false;
    }
    /**
     * 判断集合里面是否含有id的元素
     *
     * @param sourceList
     * @param uid
     * @return
     */
    public static boolean contain(List<PersonUserBean> sourceList, String uid) {
        for (PersonUserBean user : sourceList) {
            if (uid.equals(user.getUid()))
                return true;
        }
        return false;
    }
    /**
     * 判断集合里面是否含有id(tostring)的元素
     *
     * @param sourceList
     * @param uid
     * @return
     */
    public static <T>boolean containUid(List<T> sourceList, String uid) {
        for (T user : sourceList) {
            if (user.toString().equals(uid))
                return true;
        }
        return false;
    }

    /**
     * 去掉集合元素中的空元素
     *
     * @param sourceList
     * @return
     */
    public static <T>List<T> removeEmpty(List<T> sourceList) {
        for (T user : sourceList) {
            if(user==null|| TextUtils.isEmpty(user.toString())){
                sourceList.remove(user);
            }
        }
        return sourceList;
    }

    /**
     * 判断集合里面是否含有元素
     *
     * @param sourceList
     * @param userBean
     * @return
     */
    public static <T> boolean containByT(List<T> sourceList, T userBean) {
        for (T user : sourceList) {
            if (userBean.toString().equals(user.toString()))
                return true;
        }
        return false;
    }

    /**
     * 移除掉指定的元素
     *
     * @param dataList
     * @param user
     * @return
     */
    public static List<PersonUserBean> removeElement(List<PersonUserBean> dataList, PersonUserBean user) {
        if (user == null)
            return dataList;
        Iterator<PersonUserBean> sListIterator = dataList.iterator();
        while (sListIterator.hasNext()) {
            if (user.getUid().equals(sListIterator.next().getUid()))
                sListIterator.remove();
        }
        return dataList;
    }
    /**
     * 移除掉指定id的元素
     *
     * @param dataList
     * @param uid
     * @return
     */
    public static List<PersonUserBean> removeElement(List<PersonUserBean> dataList, String uid) {
        if (TextUtils.isEmpty(uid))
            return dataList;
        Iterator<PersonUserBean> sListIterator = dataList.iterator();
        while (sListIterator.hasNext()) {
            if (uid.equals(sListIterator.next().getUid()))
                sListIterator.remove();
        }
        return dataList;
    }

    public static <T> List<ChargeCheckItemBean<T>> changeList(List<T> sourceList) {
        List<ChargeCheckItemBean<T>> needList = new ArrayList<>();
        for (T source : sourceList) {
            needList.add(new ChargeCheckItemBean<>(source));
        }
        return needList;
    }

    public static <T> List<ChargeCheckItemBean<T>> AddChangeList(List<ChargeCheckItemBean<T>> sourceList, List<T> addList) {
        for (T source : addList) {
            sourceList.add(new ChargeCheckItemBean<>(source));
        }
        return sourceList;
    }

    public static <T> List<ChargeCheckItemBean<T>> AddChangeList(List<ChargeCheckItemBean<T>> sourceList, List<T> addList, Set<String> idSet) {
        for (T source : addList) {
            if (idSet.contains(source.toString()))
                sourceList.add(new ChargeCheckItemBean<>(source, true));
            else
                sourceList.add(new ChargeCheckItemBean<>(source));
        }
        return sourceList;
    }

    public static <T> List<String> ChangeListToUidList(List<T> beans) {
        List<String> uids = new ArrayList<>();
        for (T bean : beans) {
            uids.add(bean.toString());
        }
        return uids;
    }


}
